import netUtil from 'axios';
import appConfig from '../config/appConfig';

// {
//   "code": 0,
//     "msg": "success",
//     "data": {
// }
// }

// create an axios instance
const service = netUtil.create({
  baseURL: appConfig.baseURL,
  timeout: appConfig.RequestTimeout,
});

// request interceptor request拦截器
service.interceptors.request.use(
  (config) => {
    // 发生之前可以处理下：比如token等
    return config;
  },
  (error) => {
    // 请求错误
    console.log(error); // for debug
    return Promise.reject(error);
  },
);

// response 拦截器
service.interceptors.response.use(
  (response) => {
    const res = response.data;
    return res;
  },
  (error) => {
    console.log('err' + error); // for debug
    return Promise.reject(error);
  },
);

export default service;
